<?php
/**
 * 文件上传，封装ALIOSS
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/8
 * Time: 13:53
 */
class File{
    private $alioss;
    /**
     *
     * @var type 云储存上需要创建的bucket
     */
    public $attach_type = array(//模块名 => bucket名
        'bank' => 'zhengda-bank',
        'other' => 'zhengda-other',
        'benchmark' => 'zhengda-benchmark',//对标附件
        'answer' => 'zhengda-answer',//回答问题附件
    );
    public function __construct(){
        $this->alioss = new ALIOSS();
        $this->alioss->set_debug_mode(false);
    }

    /**
     * 根据内容上传
     * @param $name 文件名
     * @param $content 文件内容
     * @param  $buket 文件存放位置
     */
    public function uploadByContent($name,$content, $bucket='zhengda-other'){
        $upload_file_options = array(
            'content' => $content,
            'length' => strlen($content),
            ALIOSS::OSS_HEADERS => array(
                'Cache-control' => 'private',
            ),
        );
        $new_name = time().rand(1000,9999).'.'.$this->ext($name);
        $response = $this->alioss->upload_file_by_content($bucket,$new_name,$upload_file_options);
        if ($response->isOk()) {
            return 'http://'.$bucket.'.oss-cn-shanghai.aliyuncs.com/'.$new_name;
        }
        return false;
    }

    /**
     * 得到文件后缀
     */
    public function ext($name){
        return substr($name, strripos($name,'.')+1);
    }
    /**
     * 直接通过路径上传
     */
    public function uploadByFile($name, $file_path, $bucket='zhengda-other', $download_name=null){
        $object =  time().rand(1000,9999).'.'.$this->ext($name);
        $download_name  = $download_name ? rawurlencode($download_name) : $object;
        $option = array(
            'Content-Disposition' => "attachment;filename={$download_name}"
        );
        $response = $this->alioss->upload_file_by_file($bucket, $object,$file_path, $option);
        //print_r($response);
        if ($response->status == 200) {
            return $response->header['_info']['url'];
        }
        return false;
    }
    /**
     * 设置文件目录
     */
    public function setPath($dirname = 'question_attach'){
        $basepath = Yii::app()->basePath;
        $path = dir($path).'/assert/'.$dirname;
        if (!is_dir($path)) {
            @mkdir($path, 0777, true);
        }
        return $path;
    }
    /**
     * 获取文件目录
     */
    public function getPath($dirname = 'question_attach'){
        $basepath = Yii::app()->basePath;
        $path = dir($path).'/assert/'.$dirname;
        if (!is_dir($path)) {
            @mkdir($path, 0777, true);
        }
        return $path;
    }

    //格式化返回结果
    private function _format($response) {
        echo '|-----------------------Start---------------------------------------------------------------------------------------------------'."\n";
        echo '|-Status:' . $response->status . "\n";
        echo '|-Body:' ."\n";
        echo $response->body . "\n";
        echo "|-Header:\n";
        print_r ( $response->header );
        echo '-----------------------End-----------------------------------------------------------------------------------------------------'."\n\n";
    }

}
